package com.daJiao.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daJiao.domain.*;
import com.daJiao.entity.SysMenu;
import com.daJiao.holder.SecurityUserHolder;
import com.daJiao.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:      SysMenuController. </p>
 * <p>Description 菜单controller </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    北京医百科技</p>
 *
 * @author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
 * @since 2021/12/5 10:56
 */
@RestController
@RequestMapping(value = "/sysMenu")
@Api(tags = "菜单管理")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private SecurityUserHolder securityUserHolder;

    /**
     * <p>Title:	  get. </p>
     * <p>Description 根据id查询菜单信息</p>
     *
     * @param id String
     * @return the wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @CreateDate 2021 /12/17 19:04
     */
    @GetMapping("/{id}")
    @ApiOperation(notes = "根据id查询菜单信息", httpMethod = "GET", value = "根据id查询菜单信息")
    public Wrapper<SysMenu> get(@PathVariable String id) {
        SysMenu menu = this.sysMenuService.getById(id);
        return WrapMapper.ok(menu);
    }

    /**
     * <p>Title:	  page. </p>
     * <p>Description 分页查询菜单列表</p>
     *
     * @param pageParam the PageParam
     * @return the wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @CreateDate 2021 /12/17 19:04
     */
    @PostMapping(value = "/page")
    @ApiOperation(httpMethod = "POST", value = "分页查询菜单列表")
    public Wrapper<Page<SysMenu>> page(@ApiParam(name = "menu", value = "菜单信息") @RequestBody PageParam<SysMenu> pageParam) {
        Page<SysMenu> listPage = sysMenuService.listPage(pageParam);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, listPage);
    }

    /**
     * <p>Title:      save. </p>
     * <p>Description 保存菜单信息</p>
     *
     * @param menu the menu
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/17 19:04
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "保存菜单信息")
    public Wrapper<Integer> save(@ApiParam(name = "menu", value = "菜单信息") @RequestBody @Validated(CreateValidate.class) SysMenu menu) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        menu.setCreateBy(securityUser.getUserId());
        menu.setCreator(securityUser.getRealName());
        menu.setUpdateBy(securityUser.getUserId());
        menu.setUpdater(securityUser.getRealName());
        int result = sysMenuService.saveMenu(menu, securityUser);
        return WrapMapper.ok(result);
    }

    /**
     * <p>Title:      update. </p>
     * <p>Description 修改菜单信息</p>
     *
     * @param menu the menu
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/17 19:04
     */
    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改菜单信息")
    public Wrapper<Integer> update(@ApiParam(name = "menu", value = "菜单信息") @RequestBody @Validated(UpdateValidate.class) SysMenu menu) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        menu.setUpdateBy(securityUser.getUserId());
        menu.setUpdater(securityUser.getRealName());
        return WrapMapper.ok(sysMenuService.updateMenuInfo(menu));
    }

    /**
     * <p>Title:      delete. </p>
     * <p>Description 根据id删除菜单</p>
     *
     * @param id the id
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/17 19:04
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除菜单")
    public Wrapper<Integer> delete(@PathVariable Long id) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        return WrapMapper.ok(sysMenuService.delete(id, securityUser));
    }

    /**
     * 查询子菜单信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/tree/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询子菜单信息")
    public Wrapper<List<Tree<Long>>> tree(@PathVariable Long id) {
        List<Tree<Long>> menuTree = sysMenuService.getMenuTree(id);
        return WrapMapper.ok(menuTree);
    }

    /**
     * 查询自己拥有的菜单列表
     *
     * @return
     */
    @GetMapping(value = "/own/menus")
    @ApiOperation(httpMethod = "GET", value = "查询自己拥有的菜单列表")
    public Wrapper<List<SysMenu>> getOwnMenus() {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        List<SysMenu> ownMenus = sysMenuService.getOwnMenus(securityUser.getUserId());
        return WrapMapper.ok(ownMenus);
    }

    /**
     * 查询自己拥有的菜单树
     *
     * @return
     */
    @GetMapping(value = "/own/menuTree")
    @ApiOperation(httpMethod = "GET", value = "查询子菜单信息")
    public Wrapper<List<Tree<Long>>> getOwnTree() {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        List<Tree<Long>> menuTree = sysMenuService.getOwnTree(securityUser.getUserId());
        return WrapMapper.ok(menuTree);
    }

}
